package com.weilele.mvvm

import com.weilele.mvvm.utils.coroutine.launchThread
import com.weilele.mvvm.utils.coroutine.switchToThread
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see [Testing documentation](http://d.android.com/tools/testing)
 */
class ExampleUnitTest {

    fun testXieCheng1() {
        val timeS = System.currentTimeMillis()
        launchThread {
            val s1 = switchToThread {
                println("s1开始")
                delay(1000)
                println("s1结束")
                100
                switchToThread {
                    println("s11开始")
                    delay(1000)
                    println("s11结束")
                    110
                }
            }
            val s2 = switchToThread {
                println("s2开始")
                delay(1000)
                println("s2结束")
                100
            }
            println("time:${s1},${s2},${System.currentTimeMillis() - timeS}")
        }
        Thread.sleep(5000)
        println("main_time:${System.currentTimeMillis() - timeS}")
    }

    fun testXieCheng() {
        val timeS = System.currentTimeMillis()
        launchThread {
            val s1 = async {
                println("s1开始")
                delay(1000)
                println("s1结束")
                100
            }
            val s2 = launch {
                println("s2开始")
                delay(1000)
                println("s2结束")
                100
            }
            delay(1500)
            println("time:${s1.await()},${s1},${s2},${System.currentTimeMillis() - timeS}")
        }
        Thread.sleep(5000)
        println("main_time:${System.currentTimeMillis() - timeS}")
    }


    fun addition_isCorrect() {
        val ends = "10086".endsWith("0086")
        val startTime = System.currentTimeMillis()
        repeat(1000) {
            val ratio = getSimilarityRatio("gelulu", "geliuliu")
        }
        val offTime = System.currentTimeMillis() - startTime
        logI("花费时间：${offTime}")
    }

    /**
     * 比较两个字符串的相识度
     * 核心算法：用一个二维数组记录每个字符串是否相同，如果相同记为0，不相同记为1，每行每列相同个数累加
     * 则数组最后一个数为不相同的总数，从而判断这两个字符的相识度
     */
    private fun compare(str: String, target: String): Int {
        val d: Array<IntArray> // 矩阵
        val n = str.length
        val m = target.length
        var i = 0 // 遍历str的
        var j: Int // 遍历target的
        var ch1: Char // str的
        var ch2: Char // target的
        var temp: Int // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
        if (n == 0) {
            return m
        }
        if (m == 0) {
            return n
        }
        d = Array(n + 1) { IntArray(m + 1) }
        // 初始化第一列
        while (i <= n) {
            d[i][0] = i
            i++
        }
        // 初始化第一行
        j = 0
        while (j <= m) {
            d[0][j] = j
            j++
        }
        i = 1
        while (i <= n) {
            // 遍历str
            ch1 = str[i - 1]
            // 去匹配target
            j = 1
            while (j <= m) {
                ch2 = target[j - 1]
                temp =
                    if (ch1 == ch2 || ch1.toInt() == ch2.toInt() + 32 || ch1.toInt() + 32 == ch2.toInt()) {
                        0
                    } else {
                        1
                    }
                // 左边+1,上边+1, 左上角+temp取最小
                d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp)
                j++
            }
            i++
        }
        return d[n][m]
    }


    /**
     * 获取最小的值
     */
    private fun min(oldOne: Int, two: Int, three: Int): Int {
        var one = oldOne
        return if ((if (one < two) one else two.also { one = it }) < three) one else three
    }


    /**
     * 获取两字符串的相似度
     */
    private fun getSimilarityRatio(str: String, target: String): Float {
        val max = str.length.coerceAtLeast(target.length)
        return 1 - compare(str, target).toFloat() / max
    }
}